import { Component, ViewChild } from '@angular/core';
import { NavController } from 'ionic-angular';
import { WarnDetailsPage } from '../warn-details/warn-details'

import { WarnService } from "./services/warnService.service";
import * as $ from 'jquery';

import { Content } from 'ionic-angular';
import { ChangeDetectorRef } from '@angular/core';


import * as moment from 'moment';




@Component({
  selector: 'page-warn',
  templateUrl: 'warn.html'
})
export class WarnPage {
  alert: string = "all";


  @ViewChild(Content) content: Content;



  data: any;
  top = false;

  public deviceList: any[]
  page = 0;
  perPage = 10;
  totalData = 0;
  totalPage = 0;
  load: any[]
  status: any;
  items: any = [];
  items1: any = [];
  items2: any = [];
  // top: any;

  offset = 0;
  offset1 = 0;
  offset2 = 0;
  length = 5;

  search;
  text = "";
  show = false;
  Alltype = [];

  name;

  dataSet;

  start;
  end;

  constructor(
    public warnService: WarnService,
    public navCtrl: NavController,
    private cd: ChangeDetectorRef
  ) {

  }

  ngOnInit(){
    // this.oneWeek();
    this.queryAll('', this.end, this.start);
    this.Resolved('', this.end, this.start);
    this.Untreated('', this.end, this.start);
  }

  // ionViewWillEnter() {
  //   this.cd.detectChanges();
  //   this.oneWeek();
  // }


  ionViewWillLeave() {
    // this.items=[];
    // this.items1=[];
    // this.items2=[];
  }

  warnDetails(id: string, time, name, name1, colorName, lower, upper, message, status) {
    this.navCtrl.push(WarnDetailsPage, {
      warnId: id,
      time: time,
      name: name,
      name1: name1,
      colorName: colorName,
      lower: lower,
      upper: upper,
      message: message,
      status: status

    });
  }

  oneWeek() {
    const now = new Date();
    const nowStamp = now.getTime();
    const aDay = 3600 * 24 * 1000 * 7;;
    const startday = nowStamp - aDay;
    const startTime = new Date(startday).toLocaleString();
    const endTime = now.toLocaleString();
    const sind = startTime.indexOf('午'), eind = endTime.indexOf("午");

    const start = moment(startTime.slice(0, sind - 1) + startTime.slice(sind + 1), "YYYY/M/D HH:mm:ss").format("YYYYMMDDHHmmssSSS");
    const end = moment(endTime.slice(0, eind - 1) + endTime.slice(eind + 1), "YYYY/M/D HH:mm:ss").format("YYYYMMDDHHmmssSSS");

    this.start = start;
    this.end = end;

    this.queryAll('', start, end);
    this.Resolved('', start, end);
    this.Untreated('', start, end);

  }
  
  getlength(data){
    let length = 0;
    for (let i in data) {
      length++
    }
    return length;
  }

  queryAll(infiniteScroll, start, end) {
    const warn = Object.assign({ endTime: "20190720102400520" }, { levels: [] }, { startTime: "20170710102400520" }, { status: [] }, { types: [] });
    console.log(warn);
    var that = this;
    this.warnService.allWarn(warn, that.offset, that.length)
      .subscribe(data => {
        console.log(6666,data)
        if(data==[]){
          return;
        }
        let x = [];
        for (let i in data) {
          data[i].time = moment(data[i].time, 'YYYYMMDDHHmmssSSS').format('YYYY-MM-DD HH:mm:ss')
          x.push(data[i])
        }
        this.dataSet = [...x]
        this.status = status;
        that.items = this.items.concat(data);   // 原始+后来的
        this.offset += 5
        if (infiniteScroll) {
          infiniteScroll.complete();   /*请求完成数据更新页面*/
          if (this.getlength(data) < 5) {   /*没有数据 禁用上拉更新*/
            infiniteScroll.enable(false);
          }
        }
      }, err => console.error(err));
  }

  //已处理
  Resolved(infiniteScroll, start, end) {
    const warn = Object.assign({  endTime: "20190720102400520" }, { levels: [] }, { startTime: "20170710102400520" }, { status: [1] }, { types: []});
    var that = this;
    this.warnService.allWarn(warn, that.offset1, that.length)
      .subscribe(data => {
        console.log(7777,data)
        if(data==[]){
          return;
        }
        let x = [];
        for (let i in data) {
          data[i].time = moment(data[i].time, 'YYYYMMDDHHmmssSSS').format('YYYY-MM-DD HH:mm:ss')
          x.push(data[i])
        }
        this.dataSet = [...x]
        this.status = status;
        that.items1 = this.items1.concat(data);   // 原始+后来的
        this.offset1 += 5
        if (infiniteScroll) {
          infiniteScroll.complete();   /*请求完成数据更新页面*/
          if (this.getlength(data) < 5) {   /*没有数据 禁用上拉更新*/
            infiniteScroll.enable(false);
          }
        }
      }, err => console.error(err));
  }

  Untreated(infiniteScroll, start, end) {
    const warn = Object.assign({ endTime: "20190720102400520" }, { levels: [] }, { startTime: "20170710102400520" }, { status: [0] }, { types: [] });
    var that = this;
    this.warnService.allWarn(warn, that.offset2, that.length)
      .subscribe(data => {
        console.log(8888,data)
        if(data==[]){
          return;
        }

        let x = [];
        for (let i in data) {
          data[i].time = moment(data[i].time, 'YYYYMMDDHHmmssSSS').format('YYYY-MM-DD HH:mm:ss')
          x.push(data[i])
        }
        this.dataSet = [...x]

        this.status = status;
        that.items2 = this.items2.concat(data);   // 原始+后来的
        this.offset2 += 5
        if (infiniteScroll) {
          infiniteScroll.complete();   /*请求完成数据更新页面*/
          if (this.getlength(data)  < 5) {   /*没有数据 禁用上拉更新*/
            infiniteScroll.enable(false);
          }
        }
      }, err => console.error(err));
  }


  // //下拉刷新

  doInfinite(infiniteScroll) {
    this.queryAll(infiniteScroll, this.start, this.end);
  }


  doInfinite1(infiniteScroll) {
    this.Resolved(infiniteScroll, this.start, this.end);
  }

  doInfinite2(infiniteScroll) {
    this.Untreated(infiniteScroll, this.start, this.end);
  }



  //回到最顶部
  scrollTo() {
    this.content.scrollToTop();
  }


  // 判断回到顶部按钮
  check() {
    if (this.content.scrollTop > 200) {
      this.top = true;
    } else {
      this.top = false;
    }
    this.cd.detectChanges();

  }



}